<?php error_reporting(0);
session_start(); 
if (isset($_SESSION['connect'])) {
?>
<?PHP
include 'config.php';

$query = "SELECT * FROM  $tablename";
$result = mysql_query($query);
while($row= mysql_fetch_array($result)){
  $output = $output . $row['id'] . ", " . $row['ip'] . ", " . $row['agent'] . ", " . $row['ref'] . ", " . $row["tm"] . "\r\n";
}
$newfile="export " . date("y.m.d G:i:s") . ".txt"; 
$file = fopen ($newfile, "w"); 
fwrite($file, $output); 
fclose ($file); 
 $file = $newfile;

 if(!file)
 {
     die('file not found');
 }
 else
 {
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: TXT");
     header("Content-Transfer-Encoding: txt");
     readfile($file);
     unlink($file);
     header('Location: admin.php');
 }
 ?>
<?php
}
else
{
header('Location: login.php');
}
?>

